/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;

using DotNetNuke.Modules.Articles.Common;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Controls
{
    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.UI.Controls
    /// Project:    Articles Private Assembly
    /// Class:      Image
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Image skin object displays an article's Abstract Image.
    /// </summary>
    /// <history>
    /// 	[cnurse]	02/14/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:Image runat=\"server\"> </{0}:Image>")]
    public class Image : ArticleSkinObjectBase
    {

        #region Private Members

        private ImageAlign _Align = Constants.SETTING_ImageAlign;
        private int _MaxHeight = Constants.SETTING_ImageMaxHeight;
        private int _MaxWidth = Constants.SETTING_ImageMaxWidth;
        FileInfo _ImageFile;
        private string _ImageUrl;

        #endregion

        protected FileInfo ImageFile
        {
            get
            {
                if (_ImageFile == null)
                {
                    FileController objController = new FileController();
                    if ((ParentModule != null) && (Article != null) && (Article.AbstractImage != Null.NullInteger))
                    {
                        _ImageFile = objController.GetFileById(Article.AbstractImage, ParentModule.PortalId);
                    }
                }
                return _ImageFile;
            }
        }

        protected string ImageUrl
        {
            get
            {
                if (String.IsNullOrEmpty(_ImageUrl))
                {
                    if (ImageFile != null)
                    {
                        _ImageUrl = Globals.LinkClick("fileid=" + ImageFile.FileId.ToString(), TabId, Null.NullInteger);
                    }
                    else
                        //Add Design Time display code
                        if (DesignMode)
                            _ImageUrl = Constants.IMAGE_NotFound;

                }
                return _ImageUrl;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Tag to use for the Control
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Img; }
        }

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Alignment for the Image
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/14/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(Constants.SETTING_ImageAlign),
        Description("The Alignment for the Image")]
        public ImageAlign Align
        {
            get { return _Align; }
            set { _Align = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the root image directory for the current portal
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/14/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        public string ImageDirectory
        {
            get 
            {
                string directory = "";
                if (ParentModule != null)
                    directory = ParentModule.PortalSettings.HomeDirectory;
                return directory;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Maximum Height for the Image
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/14/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true), 
        Category("Appearance"), 
        DefaultValue(Constants.SETTING_ImageMaxHeight),
        Description("Set the Maximum Height for the Image")]
        public int MaxHeight
        {
            get { return _MaxHeight; }
            set { _MaxHeight = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Maximum Width for the Image
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/14/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true), 
        Category("Appearance"), 
        DefaultValue(Constants.SETTING_ImageMaxWidth),
        Description("Set the Maximum Width for the Image")]
        public int MaxWidth
        {
            get { return _MaxWidth; }
            set { _MaxWidth = value; }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddAttributesToRender adds attributes to the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	03/18/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            int _Width;
            int _Height;

            if (!DesignMode && ImageFile != null)
            {

                if (ImageFile.Width > ImageFile.Height)
                {
                    // Landscape
                    if (ImageFile.Width > MaxWidth)
                    {
                        _Width = MaxWidth;
                        _Height = Convert.ToInt32(ImageFile.Height * MaxWidth / ImageFile.Width);
                    }
                    else
                    {
                        _Width = ImageFile.Width;
                        _Height = ImageFile.Height;
                    }
                }
                else
                {
                    // Portrait
                    if (ImageFile.Height > MaxHeight)
                    {
                        _Width = Convert.ToInt32(ImageFile.Width * MaxHeight / ImageFile.Height);
                        _Height = MaxHeight;
                    }
                    else
                    {
                        _Width = ImageFile.Width;
                        _Height = ImageFile.Height;
                    }
                }

                //Render an image tag
                writer.AddAttribute(HtmlTextWriterAttribute.Align, Align.ToString());
                writer.AddAttribute(HtmlTextWriterAttribute.Width, _Width.ToString());
                writer.AddAttribute(HtmlTextWriterAttribute.Height, _Height.ToString());
            }
            
            writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl);

            base.AddAttributesToRender(writer);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (ImageFile != null)
                base.Render(writer);
        }

        #endregion

    }
}
